package cn.com.guage.dtm.mapping.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.mapping.domain.MappingTransTask;
import cn.com.guage.dtm.mapping.service.IMappingTransTaskService;
import cn.com.guage.dtm.metadata.domain.MdSys;
import cn.com.guage.dtm.metadata.service.IMdSysService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * 迁移作业管理Controller
 * 
 * @author liujun
 * @date 2021-07-14
 */
@Controller
@RequestMapping("/mapping/task")
public class MappingTransTaskController extends BaseController
{
    private String prefix = "mapping/task";

    @Autowired
    private IMappingTransTaskService mappingTransTaskService;
    
    @Autowired
    private IMdSysService mdSysService;

    @RequiresPermissions("mapping:task:view")
    @GetMapping()
    public String task(ModelMap mmap)
    {
    	MdSys mdSys=new MdSys();
    	List<MdSys> mdSysList = mdSysService.selectMdSysList(mdSys);
    	mmap.put("mdSysList", mdSysList);
        return prefix + "/task";
    }

    /**
     * 查询迁移作业管理列表
     */
    @RequiresPermissions("mapping:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MappingTransTask mappingTransTask)
    {
        startPage();
        List<MappingTransTask> list = mappingTransTaskService.selectMappingTransTaskList(mappingTransTask);
        return getDataTable(list);
    }

    /**
     * 导出迁移作业管理列表
     */
    @RequiresPermissions("mapping:task:export")
    @Log(title = "迁移作业管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MappingTransTask mappingTransTask)
    {
        List<MappingTransTask> list = mappingTransTaskService.selectMappingTransTaskList(mappingTransTask);
        ExcelUtil<MappingTransTask> util = new ExcelUtil<MappingTransTask>(MappingTransTask.class);
        return util.exportExcel(list, "迁移作业管理数据");
    }

    /**
     * 新增迁移作业管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存迁移作业管理
     */
    @RequiresPermissions("mapping:task:add")
    @Log(title = "迁移作业管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MappingTransTask mappingTransTask)
    {
        return toAjax(mappingTransTaskService.insertMappingTransTask(mappingTransTask));
    }

    /**
     * 修改迁移作业管理
     */
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        MappingTransTask mappingTransTask = mappingTransTaskService.selectMappingTransTaskById(taskId);
        mmap.put("mappingTransTask", mappingTransTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存迁移作业管理
     */
    @RequiresPermissions("mapping:task:edit")
    @Log(title = "迁移作业管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MappingTransTask mappingTransTask)
    {
        return toAjax(mappingTransTaskService.updateMappingTransTask(mappingTransTask));
    }

    /**
     * 删除迁移作业管理
     */
    @RequiresPermissions("mapping:task:remove")
    @Log(title = "迁移作业管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mappingTransTaskService.deleteMappingTransTaskByIds(ids));
    }
    
    
    
    /**
     * 生成代码（下载方式）
     */
    @RequiresPermissions("mapping:task:edit")
    @Log(title = "代码生成", businessType = BusinessType.GENCODE)
    @GetMapping("/download/{taskId}")
    public void download(HttpServletResponse response, @PathVariable("taskId") Long taskId) throws IOException
    {
        byte[] data = mappingTransTaskService.downloadCode(taskId);
        genCode(response, data);
    }
    
    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"dtm_task.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
    
}
